package com.owbtgad.common.utils;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;

public class ActivityUtils {
    public static final String KEY_HEAD = "links://com.chzsyh.pdfve";
    public static final String KEY_WEB_AND = "齾";
    public static final String KEY_WEB_AND2="龘";
    private static String TAG = "ActivityUtils";

    /**
     * 通过链接打开页面
     *
     * @param url     地址
     * @param isLogIn true需要登录
     */
    public static void showActivity(String url, boolean isLogIn) {
        try {
            if (!url.isEmpty()) {
                if (url.startsWith(KEY_HEAD)) {
                    Uri testUriMix = Uri.parse(url);
                    if (isLogIn) {
                        ARouter.getInstance().build(testUriMix)
                                .navigation();
                    } else {
                        ARouter.getInstance().build(testUriMix).greenChannel()
                                .navigation();
                    }
                } else {
                    Uri testUriMix = Uri.parse(KEY_HEAD + url);
                    if (isLogIn) {
                        ARouter.getInstance().build(testUriMix)
                                .navigation();
                    } else {
                        ARouter.getInstance().build(testUriMix).greenChannel()
                                .navigation();
                    }
                }
            }
        } catch (Exception e) {

        }


    }

    /**
     * 带返回
     *
     * @param url
     * @param isLogIn
     * @param activity
     * @param code
     */
    public static void showActivity(String url, boolean isLogIn, Activity activity, int code) {
        if (!url.isEmpty()) {
            if (url.startsWith(KEY_HEAD)) {
                Uri testUriMix = Uri.parse(url);
                if (isLogIn) {
                    ARouter.getInstance().build(testUriMix)
                            .navigation(activity, code);
                } else {
                    ARouter.getInstance().build(testUriMix).greenChannel()
                            .navigation(activity, code);
                }
            } else {
                Uri testUriMix = Uri.parse(KEY_HEAD + url);
                if (isLogIn) {
                    ARouter.getInstance().build(testUriMix)
                            .navigation(activity, code);
                } else {
                    ARouter.getInstance().build(testUriMix).greenChannel()
                            .navigation(activity, code);
                }
            }
        }

    }

    /**
     * 通过链接打开页面
     *
     * @param url 链接
     */
    public static void showActivity(String url) {
        showActivity(url, true);
    }

    public static void startMain() {
        startMain(0, new Bundle());
    }

    public static void startMain(int type) {
        startMain(type, new Bundle());
    }

    /**
     * 先打开首页再跳转到对应页面
     *
     * @param type   操作类型
     * @param bundle 传数据
     */
    public static void startMain(int type, Bundle bundle) {
        ARouter.getInstance().build(ARouterString.MAIN_ACTIVITY).withBundle("bundle", bundle).withInt("type", type).greenChannel().navigation();
    }

    /**
     * 注册
     */
    public static void startSignIn() {
        startSignIn(0);
    }

    /**
     * 跳转登录注册
     *
     * @param type 0登陆后跳首页 不然返回当前页
     */
    public static void startSignIn(int type) {
        ARouter.getInstance().build(ARouterString.SIGN_IN_ACTIVITY).withInt("type", type).greenChannel().navigation();
    }

    /**
     * 登录跳转
     *
     * @param uri
     */
    public static void startSignIn(Uri uri) {
        ARouter.getInstance().build(ARouterString.SIGN_IN_ACTIVITY).withParcelable("uri", uri).greenChannel().navigation();
    }

    /**
     * web
     */
    public static void startWebViewActivity(String url, String title) {
        startWebViewActivity(url, title, false);
    }

    public static void startWebViewActivity(String url, boolean isLogin) {
        startWebViewActivity(url, "", isLogin);
    }

    /**
     * @param url
     * @param title
     * @param isLogin 是否需要登录 true是
     */
    public static void startWebViewActivity(String url, String title, boolean isLogin) {
        if (url.startsWith(KEY_HEAD)) {
            showActivity(url, isLogin);
        } else {
            if (!url.contains("http")) {
                if (url.contains("/h5/")) {
                    url = UrlHelp.getBsseUrl() + url;
                }
            }

            url = url.replaceAll("&", KEY_WEB_AND);
            url = url.replaceAll("#",KEY_WEB_AND2);
            Log.d(TAG, "startWebViewActivity: " + url);
            showActivity(ARouterString.WEB_ACTIVITY + "?title=" + title + "&url=" + url, isLogin);
        }
    }

    /**
     * web
     */
    public static void startWebViewActivity(String url) {
        startWebViewActivity(url, "");
    }


}
